/*
 * event.c
 *
 *  Created on: May 21, 2012
 *      Author: root
 */
#include "unp.h"
#include "ftp_client.h"

USER_CMD event_open(USER_CMD cmd_info) {

	if (cmd_info.cmd_num == 1) {
		printf("(to)");
		fflush(stdout);

		Fgets(cmd_info.arg1, MAXLINE, stdin);

	}
	parse_LF(cmd_info.arg1);
	strcpy(first_ip, cmd_info.arg1);
	strcpy(ftp_serv_ip, cmd_info.arg1);
	cmd_info.sockfd = tcp_connect(cmd_info.arg1, "21");
	get_myaddress(cmd_info.sockfd);

	print_response(cmd_info.sockfd);

	printf("Name (%s:np2012):", cmd_info.arg1);
	fflush(stdout);

	Fgets(cmd_info.arg1, MAXLINE, stdin);
	parse_LF(cmd_info.arg1);
	send_request(cmd_info.sockfd, "USER", cmd_info.arg1);

	print_response(cmd_info.sockfd);

	return (cmd_info);

}

void event_lcd(USER_CMD cmd_info) {
	char directory[1000];
	if (cmd_info.cmd_num == 1) {
		getcwd(directory, 1000);
		printf("Local directory now %s\n", directory);
	}
	if (cmd_info.cmd_num == 2) {
		if (chdir(cmd_info.arg1) == 0) {
			getcwd(directory, 1000);
			printf("Local directory now %s\n", directory);
		} else {
			printf("local: %s: No such file or directory\n", cmd_info.arg1);
		}
	}

}

void event_passive() {
	if (passive_bit == 0) {
		printf("Passive mode on\n");

		passive_bit = 1;
	} else {
		printf("Passive mode off\n");
		passive_bit = 0;
	}
}

void event_debug() {
	if (toggle == 0) {
		printf("Debugging on (debug=1)\n");
		toggle = 1;
	} else {
		printf("Debugging off (debug=0)\n");
		toggle = 0;
	}
}

void event_user(USER_CMD cmd_info) {
	if (cmd_info.cmd_num == 1) {
		printf("(username)");
		fflush(stdout);
		Fgets(cmd_info.arg1, MAXLINE, stdin);
		parse_LF(cmd_info.arg1);
	}

	send_request(cmd_info.sockfd, "USER", cmd_info.arg1);
	print_response(cmd_info.sockfd);
}

void event_cd(USER_CMD cmd_info) {
	if (cmd_info.cmd_num == 1) {
		printf("(remote-directory)");
		fflush(stdout);
		Fgets(cmd_info.arg1, MAXLINE, stdin);

	}
	parse_LF(cmd_info.arg1);
	send_request(cmd_info.sockfd, "CWD", cmd_info.arg1);

	state = S_REPLY;
}

int event_ls_dir_nlist(USER_CMD cmd_info) /////list,dir
{
	if (type_bit == 0) {
		send_request(cmd_info.sockfd, "TYPE A", "");
		type_bit = 1;

		return (S_REPLY1);

	} else {

		if (passive_bit == 0) {

			send_request(cmd_info.sockfd, "PORT", PORT_arg);
		} else if (passive_bit == 1) {

			send_request(cmd_info.sockfd, "PASV", "");
		}
		return (S_REPLY2);
	}
}

USER_CMD event_get_put(USER_CMD cmd_info)
{
	if (cmd_info.cmd_num == 1) {
		printf("(remote-file) ");
		fflush(stdout);
		Fgets(cmd_info.arg1, MAXLINE, stdin);

		parse_LF(cmd_info.arg1);

		printf("(local-file) ");
		fflush(stdout);
		Fgets(cmd_info.arg2, MAXLINE, stdin);

		parse_LF(cmd_info.arg2);

		if (!strcmp(cmd_info.cmd, "get"))
			printf("local: %s  remote: %s\n", cmd_info.arg2, cmd_info.arg1);
		if (!strcmp(cmd_info.cmd, "put"))
			printf("local: %s  remote: %s\n", cmd_info.arg1, cmd_info.arg2);
	}

	if (cmd_info.cmd_num == 2) {
		parse_LF(cmd_info.arg1);

		strcpy(cmd_info.arg2, cmd_info.arg1);

		printf("local: %s  remote: %s\n", cmd_info.arg2, cmd_info.arg1);



	}

	if (cmd_info.cmd_num == 3) {

		parse_LF(cmd_info.arg1);

		parse_LF(cmd_info.arg2);

		if (!strcmp(cmd_info.cmd, "get"))
			printf("local: %s  remote: %s\n", cmd_info.arg2, cmd_info.arg1);
		if (!strcmp(cmd_info.cmd, "put"))
			printf("local: %s  remote: %s\n", cmd_info.arg1, cmd_info.arg2);

	}

	if (!strcmp(cmd_info.cmd, "put")) {
			FILE *fp = fopen(cmd_info.arg1, "rb");
			if (fp == NULL) {
				printf("local: %s : No such file or directory\n",cmd_info.arg1);
				state=U_CMD;
				return(cmd_info);
			} else {
				fclose(fp);
			}
		}





	if (passive_bit == 0) {

		send_request(cmd_info.sockfd, "PORT", PORT_arg);
	} else if (passive_bit == 1) {

		send_request(cmd_info.sockfd, "PASV", "");

	}
	state=S_REPLY2;
	return (cmd_info);

}

void event_type() {
	if (type_bit == 0) {
		printf("Using binary mode to transfer files\n");
	} else {
		printf("Using ascii mode to transfer files\n");
	}
}
